package com.lolc.ihm.component;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxEditor;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.lolc.ihm.Constants;
import com.lolc.ihm.MainWindow;

public class ComboBoxEditorRenderer implements ComboBoxEditor {

	private EditorPane editorPane;

	public ComboBoxEditorRenderer() {
		editorPane = new EditorPane();
		
		//this.addActionListener(l); (fcsListener);	
	}

	@Override
	public EditorPane getEditorComponent() {
		return editorPane;
	}

	@Override
	public void setItem(Object anObject) {
		editorPane.setText(anObject == null ? null : anObject.toString());
	}

	@Override
	public Object getItem() {
		return editorPane.getText();
	}

	@Override
	public void selectAll() {
		editorPane.selectAll();
	}

	@Override
	public void addActionListener(ActionListener l) {
		editorPane.addActionListener(l);
	}

	@Override
	public void removeActionListener(ActionListener l) {
		editorPane.removeActionListener(l);
	}	
	    
	public class EditorPane extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JTextField field;
		private JLabel icon;
		/**
		 * @return the field
		 */
		public JTextField getField() {
			return field;
		}

		/**
		 * @param field the field to set
		 */
		public void setField(JTextField field) {
			this.field = field;
		}

		private ImageIcon iconImg; 

		public EditorPane() {
			setLayout(new GridBagLayout());

			GridBagConstraints gbc = new GridBagConstraints();
			gbc.weightx = 1;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 0;

			field = new JTextField();
			//field.setFont(Constants.comboBoxEditorFont);
			field.setBorder(BorderFactory.createEmptyBorder());
			this.add(field, gbc);

			GridBagConstraints iconConstraint = new GridBagConstraints();
			iconConstraint.weightx = 0;
			iconConstraint.fill = GridBagConstraints.NONE;
			iconConstraint.gridx = 1;

			iconImg = new ImageIcon(MainWindow.class.getResource(Constants.ICON_REMOVE_ITEM_GREY));
			icon = new JLabel(iconImg);
			icon.addMouseListener(new MouseListener() {

				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					System.out.println("released");
				}

				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					System.out.println("pressed");
				}

				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					iconImg = new ImageIcon(MainWindow.class.getResource(Constants.ICON_REMOVE_ITEM_GREY));
					icon = new JLabel(iconImg);
					icon.paintImmediately(icon.getBounds());
					//	System.out.println("mouse exited");
				}

				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					iconImg = new ImageIcon(MainWindow.class.getResource(Constants.ICON_REMOVE_ITEM));
					icon = new JLabel(iconImg);
					//System.out.println("mouse entered");
					//add(icon, iconConstraint);
				}

				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					setText(null);
				}
			});

			this.add(icon, iconConstraint);
		}

		@Override
		public void addNotify() {
			super.addNotify(); 
			field.requestFocusInWindow();
		}

		public void selectAll() {
			field.selectAll();
		}

		public void setText(String text) {
			field.setText(text);
		}

		public String getText() {
			return field.getText();
		}

		public void addActionListener(ActionListener listener) {
			field.addActionListener(listener);
		}

		public void removeActionListener(ActionListener listener) {
			field.removeActionListener(listener);
		}


		/**
		 * @return the icon
		 */
		public JLabel getIcon() {
			return icon;
		}

		/**
		 * @return the iconImg
		 */
		public ImageIcon getIconImg() {
			return iconImg;
		}

		/**
		 * @param icon the icon to set
		 */
		public void setIcon(JLabel icon) {
			this.icon = icon;
		}

		/**
		 * @param iconImg the iconImg to set
		 */
		public void setIconImg(ImageIcon iconImg) {
			this.iconImg = iconImg;
		}

	}
}
